<template>
    <div class="news-classify">
      <div style="display:flex;align-items: center;margin:0 0 10px;">
        <el-input style="width:200px;margin-right:20px;" clearable size="small" v-model="classifyName" placeholder="请输入分类名称"></el-input>
        <el-select style="margin-right:20px;" clearable size="small" v-model="goodsState" placeholder="请选择">
          <el-option
            v-for="item in [{
              value: '1',
              label: '启用'
            },{
              value: '0',
              label: '禁用'
            },]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-button size="small" type="primary" @click="fetchData">查询</el-button>
        <el-button size="small" type="primary" @click="add">新建商品分类</el-button>
      </div>
      <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row empty-text="暂无数据">
        <!-- <el-table-column label="ID" width="150">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column> -->
        <el-table-column label="分类名称">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="分类排序" width="150">
          <template slot-scope="scope">
            {{ scope.row.sort }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="200">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100">
          <template slot-scope="scope">
            <el-tag style="cursor:pointer;" size="small" v-if="scope.row.state == 0" type="danger">禁用</el-tag>
            <el-tag style="cursor:pointer;" size="small" v-else type="success">启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scope">
            <el-button @click="changeState(scope.row)" type="text" style="color:#ff0000;" v-if="scope.row.state == 1">禁用</el-button>
            <el-button @click="changeState(scope.row)" type="text" v-else>启用</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button @click="edit(scope.row)" type="text">编辑</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-popconfirm
              @confirm="del(scope.row)"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="是否删除本条数据？">
              <el-button  slot="reference" type="text" style="color:#ff0000;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin:10px 0 0 0;text-align:right;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 30, 50, 100]"
          :page-size="1"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageObj.total">
        </el-pagination>
      </div>
  
      <el-dialog :title="dailogTitle" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item style="margin-bottom:20px;" label="分类名称" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off" placeholder="请输入分类名称"></el-input>
          </el-form-item>
          <el-form-item label="分类排序" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off" placeholder="请输入分类名称"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import {
    getClassifyListApi,
    saveClassifyApi,
    changeClassifyStateApi,
    deleteClassifyApi,
    getClassifyDetailApi
  } from '@/api/goods'
  
  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        list: [],
        listLoading: true,
        dialogFormVisible: false,
        dailogTitle: '新建分类',
        form: {
          name: '',
          sort: ''
        },
        formLabelWidth: '80px',
        currentPage: 1,
        pageObj: {
          pageSize: 10,
          pageNum: 1,
          total: 0
        },
        saveId: '',
        goodsState: '',
        classifyName: ''
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      onSubmit() {
        this.fetchData()
      },
      del(row) {
        if(row.state == 1) {
          this.$message.error('启用状态禁止删除！')
          return
        }
        deleteClassifyApi({
          ids: row.id
        }).then((res) => {
          this.fetchData()
        })
      },
      add() {
        this.dialogFormVisible = true
        this.form.name = ''
        this.dailogTitle = '新建分类'
      },
      edit(row) {
        this.dailogTitle = '编辑分类'
        this.saveId = row.id
        getClassifyDetailApi(row.id).then((res) => {
          this.dialogFormVisible = true
          this.form.name = res.data.name
          this.form.sort = res.data.sort
        })
      },
      fetchData() {
        this.listLoading = true
        getClassifyListApi({
          source: 'score',
          name: this.classifyName,
          state: this.goodsState,
          pageSize: this.pageObj.pageSize,
          pageNum: this.pageObj.pageNum
        }).then(response => {
          this.list = response.data.result
          this.pageObj.total = response.data.total
          this.listLoading = false
        })
      },
      handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.pageObj.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.pageObj.pageNum = val
        this.fetchData()
      },
      save() {
        saveClassifyApi({
          id: this.saveId,
          source: 'score',
          name: this.form.name,
          sort: this.form.sort
        }).then((res) => {
          this.$message.success('操作成功！')
          this.cancel()
          this.fetchData()
        })
      },
      cancel() {
        this.saveId = ''
        this.dialogFormVisible = false
      },
      changeState(row) {
        changeClassifyStateApi({
          id: row.id,
          state: row.state == 1? 0: 1
        }).then((res) => {
          this.$message.success('操作成功！')
          this.fetchData()
        })
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
     .news-classify{
       padding: 20px;
       .el-form-item{
         margin-bottom: 0;
       }
     }
  </style>
  